
from . import ConnPG
import psycopg2


class AddUser:

    async def add_user(self, name, phone, password):#添加用户和密码

        #参数检查
        if (not all((name, password, phone)) or len(password)>32 
                                      or len(name)>32
                                      or name.isspace() 
                                      or password.isspace()
                                      or len(phone) != 11
                                      ):

            return {
                'status': -1,
                'errorCode': 100100,
                'errorMsg': '账号密码不能为空或太长（超过32位）',
                'resultBody': None
            }

        sql = 'INSERT INTO users(name, password, phone) VALUES (%s, %s, %s)'

        try:
            await ConnPG().execute(sql, (name, password, phone))
        except psycopg2.errors.UniqueViolation:

            return {

                'status': -1,
                'errorCode': 100101,
                'errorMsg': '用户名或电话已存在',
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "用户添加成功",
            }

    async def update_user(self, user_id, new_name):#更改用户名

        #参数检查
        if user_id == None or not user_id.isdecimal() or new_name == None:

            return {
                'status': -1,
                'errorCode': 100100,
                'errorMsg': '接口有两个个必传参数，其中user_id为整数',
                'resultBody': None
            }

        sql = "UPDATE users SET name=%s WHERE id=%s"

        await ConnPG().execute(sql, (new_name, user_id))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': f"用户name更新为：{new_name}",
        }


    async def del_user(self, user_id):#删除用户

        #参数检查
        if user_id == None or not user_id.isdecimal():



            return {
                'status': -1,
                'errorCode': 100100,
                'errorMsg': '接口有一个必传参数且user_id为整数',
                'resultBody': None
            }

        sql = "DELETE FROM users WHERE id=%s"

        try:
            await ConnPG().execute(sql, (user_id,))
        except psycopg2.errors.ForeignKeyViolation as e:

            print(e)

            return {

                'status': -1,
                'errorCode': 100112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "用户删除成功",
            }

    async def update_phone(self, user_id, phone):#删除用户

        #参数检查
        if not all([user_id, phone]) or user_id == None or not user_id.isdecimal() or not phone.isdecimal():



            return {
                'status': -1,
                'errorCode': 100100,
                'errorMsg': '接口有两个必传参数且user_id，phone为整数',
                'resultBody': None
            }

        # sql = "DELETE FROM users WHERE id=%s"
        sql = "UPDATE users SET phone=%s WHERE id=%s"



        await ConnPG().execute(sql, (phone, user_id,))

    
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "电话修改成功",
        }